package chrisbode.cps142.a1;

import java.util.ArrayList;
import java.util.Scanner;

public class DateBook {
	public static void main(String[] args) {
		Scanner console = new Scanner(System.in);
		ArrayList<TimeOfDay> times = new ArrayList<TimeOfDay>();
		Appointment[] appointments = new Appointment[0];

		boolean addAppointments = true;

		System.out.println("Enter appointment details when prompted.");

		while (addAppointments) {
			System.out.print("Description: ");
			String description = console.nextLine();

			TimeOfDay time = null;

			while (time == null) {
				System.out.print("Time (HH:MM:SS): ");
				String timeString = console.nextLine();

				time = TimeOfDay.createFromString(timeString);
			}

			if (!times.contains(time)) {
				appointments = DateBook.append(new Appointment(description,
						time), appointments);
				times.add(time.copy()); // Copied so when times are incremented,
										// the times in the ArrayList do not
										// change.
				System.out.println("Appointment added for " + time + ".");
			} else {
				System.out
						.println("You already have an appointment scheduled at "
								+ time + ". The appointment was not added.");
			}

			System.out.print("More appointments? (y/n): ");

			if (!(console.nextLine().equalsIgnoreCase("y"))) {
				addAppointments = false;
			}
		}

		System.out.println("Done adding appointments.\n");

		formatAppointments(times, appointments);
		
		for (Appointment a : appointments) {
			for (int i = 0; i < 30; i++) {
				TimeOfDay t = a.getTime();
				t.incrementOneMinute();
				a.setTime(t);
			}
		}
		
		System.out.println("\n");
		
		System.out.println("With time incremented by 30 minutes: \n");

		formatAppointments(times, appointments);
	}

	/**
	 * Appends an appointment to the end of an existing array of type
	 * Appointment[]
	 * 
	 * @param newAppointment
	 * @param appointments
	 * @return A new array with the new appointment appended.
	 */
	private static Appointment[] append(Appointment newAppointment,
			Appointment[] appointments) {
		Appointment[] newAppointments = new Appointment[appointments.length + 1];
		for (int i = 0; i < appointments.length; i++) {
			newAppointments[i] = appointments[i];
		}

		newAppointments[newAppointments.length - 1] = newAppointment;
		return newAppointments;
	}
	
	/**
	 * Displays the contents of the ArrayList and Array per the assignment's instructions.
	 * @param times
	 * @param appointments
	 */
	private static void formatAppointments(ArrayList<TimeOfDay> times, Appointment[] appointments) {
		if (times.size() > 0) {
			System.out.println("Appointments are scheduled for these times:");
			System.out.print("\t");

			for (int i = 0; i < times.size(); i++) {
				if (i != 0)
					System.out.print(", ");
				System.out.print(times.get(i));
			}

			System.out.print("\n\n");

			System.out.println("Full text: ");
			for (Appointment app : appointments) {
				System.out.println("\t" + app);
			}
		} else {
			System.out.println("You scheduled no appointments.");
		}
	}
}
